﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling;
using devtm.Editor.TypeSelector;
using Mono.Cecil;



namespace devtm.AutoMapper
{
    public partial class TypeElement
    {

        //public Type Type
        //{
        //    get
        //    {

        //        Type t = MappingModel.FindType(AssemblyQualifiedName);

        //        return t;

        //    }
        //}

        public void RefreshMappers(bool refresh, CustomTypeRepository repository)
        {

            if (NewMapperHelper.IsBusy)
                return;

            foreach (MappingDestination item in this.DMappings)
                if (item.Mapper != null && item.Mapper.MappingSource != null && item.Mapper.MappingSource.TypeElement != null && !string.IsNullOrEmpty(item.Mapper.MappingSource.TypeElement.AssemblyQualifiedName))
                    Mapper.UpdateProperties(item.Mapper.MappingSource, refresh, repository);

            foreach (MappingSource item in this.SMappings)
                if (item.Mapper != null && item.Mapper.MappingSource != null && !string.IsNullOrEmpty(item.Mapper.MappingSource.TypeElement.AssemblyQualifiedName))
                    Mapper.UpdateProperties(item, refresh,  repository);

        }


        #region AssemblyQualifiedName

        private string _AssemblyQualifiedName = string.Empty;
        private string _AssemblyQualifiedNameUltraShort = string.Empty;
        internal string GetAssemblyQualifiedNameValue()
        {
            return _AssemblyQualifiedName ?? string.Empty;
        }


        internal void SetAssemblyQualifiedNameValue(string newValue)
        {
            
            _AssemblyQualifiedName = newValue;

            if (!string.IsNullOrEmpty(newValue))
            {

                using (CustomTypeRepository repository = CustomTypeRepository.Instance(this.Store, false))
                {
                    if (repository.ContainingProject != null)
                    {

                        TypeDefinition type = repository.GetType(_AssemblyQualifiedName);

                        if (type != null)
                        {

                            bool test = false;

                            Action<object, System.ComponentModel.PropertyChangedEventArgs> act = (a, b) =>
                            {
                                test = true;
                            };

                            System.ComponentModel.PropertyChangedEventHandler action = new System.ComponentModel.PropertyChangedEventHandler(act);

                            try
                            {

                                PropertyChanged += action;

                                using (Transaction trans = Store.TransactionManager.BeginTransaction("Update Type"))
                                {

                                    if (type != null)
                                    {
                                        Namespace = type.Namespace;
                                        FullName = type.FullName;
                                        Name = type.Name;
                                    }
                                    else
                                    {
                                        Namespace = String.Empty;
                                        FullName = String.Empty;
                                        Name = String.Empty;
                                    }

                                    RefreshMappers(false, repository);

                                    if (test)
                                        trans.Commit();

                                }

                            }
                            finally
                            {
                                PropertyChanged -= action;
                            }
                        }

                    }                    
                }

                return;
            }

            using (Transaction trans = Store.TransactionManager.BeginTransaction("Update Type"))
            {

                Namespace = String.Empty;
                FullName = String.Empty;
                Name = String.Empty;
                trans.Commit();
            }
        }

        #endregion

        /// <summary>
        /// AssemblyQualifiedName without version
        /// </summary>
        public string AssemblyQualifiedNameUltraShort
        {
            get
            {

                if (string.IsNullOrEmpty(_AssemblyQualifiedNameUltraShort) | !_AssemblyQualifiedNameUltraShort.StartsWith(_AssemblyQualifiedName))
                    if (!string.IsNullOrEmpty(_AssemblyQualifiedName))
                    {

                        var tb = _AssemblyQualifiedName.Split(',');
                        _AssemblyQualifiedNameUltraShort = tb[0].Trim();

                        if (tb.Length >= 2)
                        {
                            _AssemblyQualifiedNameUltraShort += ", ";
                            _AssemblyQualifiedNameUltraShort += tb[1].Trim();
                        }

                    }
                    else
                        _AssemblyQualifiedNameUltraShort = string.Empty;
                    
                return _AssemblyQualifiedNameUltraShort;

            }
        }



    }
}
